package com.webproject.shop.common;

import com.webproject.shop.common.StringUtils;

public class Check {
    /*
     NUM 数字
     SMALL_LETTER 小写字母
     CAPITAL_LETTER 大写字母
     OTHER_CHAR  特殊字符
     */
    private static final int NUM = 1;
    private static final int SMALL_LETTER = 2;
    private static final int CAPITAL_LETTER = 3;
    private static final int OTHER_CHAR = 4;

    //检测密码字符类型
    private static int checkCharacterType(char c) {
        if (c >= 48 && c <= 57) {
            return NUM;
        }
        if (c >= 65 && c <= 90) {
            return CAPITAL_LETTER;
        }
        if (c >= 97 && c <= 122) {
            return SMALL_LETTER;
        }
        return OTHER_CHAR;
    }

    private static int countLetter(String passwd, int type) {
        int count = 0;
        if (null != passwd && passwd.length() > 0) {
            for (char c : passwd.toCharArray()) {
                if (checkCharacterType(c) == type) {
                    count++;
                }
            }
        }
        return count;
    }

    //检查密码强度
    public static int checkPasswordStrength(String passwd) {

        int level = 0;

        //判断密码是否含有数字有level++
        if (countLetter(passwd, NUM) > 0) {
            level++;
        }
        //判断密码是否含有小写字母有level++
        if (countLetter(passwd, SMALL_LETTER) > 0) {
            level++;
        }
        //判断密码是否还有大写字母有level++
        if (countLetter(passwd, CAPITAL_LETTER) > 0) {
            level++;
        }
        //判断密码是否还有特殊字符有level++
        if (countLetter(passwd, OTHER_CHAR) > 0) {
            level++;
        }
        return level;
    }

}

//返回值为1对应弱，2对应中等，3对应强，4对应非常强
